package com.ruoyi.jwyz.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.jwyz.domain.Activity;
import com.ruoyi.jwyz.service.IActivityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jwyz.domain.UserAttendActivity;
import com.ruoyi.jwyz.service.IUserAttendActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户参与约会(User_Attend_Activity)Controller
 *
 * @Author lxp
 * @Date 2023-03-31
 */
@Api(tags = "用户参与约会(User_Attend_Activity)", value = "用户参与约会(User_Attend_Activity)管理")
@RestController
@RequestMapping("/jwyz/activities")
public class UserAttendActivityController extends BaseController {
    @Autowired
    private IUserAttendActivityService userAttendActivityService;

    @Autowired
    private IActivityService activityService;

    /**
     * 查询用户参与约会(User_Attend_Activity)列表
     */
    @ApiOperation("获取用户参与约会(User_Attend_Activity)列表")
//    @PreAuthorize("@ss.hasPermi('jwyz:activities:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserAttendActivity userAttendActivity) {
        startPage();
        List<UserAttendActivity> list = userAttendActivityService.selectUserAttendActivityList(userAttendActivity);
        return getDataTable(list);
    }

    /**
     * 导出用户参与约会(User_Attend_Activity)列表
     */
    @ApiOperation("导出用户参与约会(User_Attend_Activity)列表")
//    @PreAuthorize("@ss.hasPermi('jwyz:activities:export')")
    @Log(title = "用户参与约会(User_Attend_Activity)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAttendActivity userAttendActivity) {
        List<UserAttendActivity> list = userAttendActivityService.selectUserAttendActivityList(userAttendActivity);
        ExcelUtil<UserAttendActivity> util = new ExcelUtil<UserAttendActivity>(UserAttendActivity.class);
        util.exportExcel(response, list, "用户参与约会(User_Attend_Activity)数据");
    }

    /**
     * 获取用户参与约会(User_Attend_Activity)详细信息
     */
    @ApiOperation("获取用户参与约会(User_Attend_Activity)详细信息-后台")
//    @PreAuthorize("@ss.hasPermi('jwyz:activities:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(name = "id", value = "用户参与约会(User_Attend_Activity)ID", required = true)
                              @PathVariable("id") Long id) {
        return success(userAttendActivityService.selectUserAttendActivityById(id));
    }

    /**
     * 新增用户参与约会(User_Attend_Activity)
     */
    @ApiOperation("新增用户参与约会(User_Attend_Activity)")
    @Anonymous
    @Log(title = "用户参与约会(User_Attend_Activity)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAttendActivity userAttendActivity) {
        return toAjax(userAttendActivityService.insertUserAttendActivity(userAttendActivity));
    }

    /**
     * 修改用户参与约会(User_Attend_Activity)
     */
    @ApiOperation("修改用户参与约会(User_Attend_Activity)")
//    @PreAuthorize("@ss.hasPermi('jwyz:activities:edit')")
    @Log(title = "用户参与约会(User_Attend_Activity)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAttendActivity userAttendActivity) {
        return toAjax(userAttendActivityService.updateUserAttendActivity(userAttendActivity));
    }

    /**
     * 删除用户参与约会(User_Attend_Activity)
     */
    @ApiOperation("删除用户参与约会(User_Attend_Activity)")
//    @PreAuthorize("@ss.hasPermi('jwyz:activities:remove')")
    @Log(title = "用户参与约会(User_Attend_Activity)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(name = "ids", value = "用户参与约会(User_Attend_Activity)IDs", required = true)
                             @PathVariable Long[] ids) {
        return toAjax(userAttendActivityService.deleteUserAttendActivityByIds(ids));
    }

    /**
     * 获取用户参与约会(User_Attend_Activity)对应的活动信息
     */

    @ApiOperation("获取用户参与约会(User_Attend_Activity)对应的活动信息")
    @GetMapping("/getActivity/{uid}")
    public AjaxResult getActivity(@PathVariable("uid") String uId) {
        Long userId = Long.parseLong(uId);
        QueryWrapper<UserAttendActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<UserAttendActivity> list = userAttendActivityService.list(queryWrapper);
        List<Activity> activityList = new ArrayList<>();
        for (UserAttendActivity userAttendActivity : list) {
            QueryWrapper<Activity> activityQueryWrapper = new QueryWrapper<>();
            activityQueryWrapper.eq("id", userAttendActivity.getActivityId());
            activityList.addAll(activityService.list(activityQueryWrapper));
        }
        if (activityList.size() != 0) {
            return success(activityList);
        }
        return error("没有查到您报名的相关活动信息");
    }


}

